package com.utaedang.jesseclub.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.content.Context;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.PowerManager;
import android.telephony.TelephonyManager;
import android.view.Display;

public class Function {
	final static int BUFFER_SIZE = 1024 * 1000;

	public static class AlarmWakeLock {
		private static final String TAG = "AlarmWakeLock";
		private static PowerManager.WakeLock mWakeLock;

		public static void wakeLock(Context context) {
			if (mWakeLock != null) {
				return;
			}

			PowerManager powerManager = (PowerManager) context
					.getSystemService(Context.POWER_SERVICE);

			mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
					TAG);
			mWakeLock.acquire();
		}

		public static void releaseWakeLock() {
			if (mWakeLock != null) {
				mWakeLock.release();
				mWakeLock = null;
			}
		}
	}
	
	public static String getAudioStatus(Context context){
		AudioManager clsAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
		String status = "";
		switch( clsAudioManager.getRingerMode( ) )
		{
		   case AudioManager.RINGER_MODE_VIBRATE:
		    // 진동 모드
			   status = "VIBRATE";
			    break;
		   case AudioManager.RINGER_MODE_NORMAL:
			   status = "NORMAL";
		    // 소리 모드
			    break;
		   case AudioManager.RINGER_MODE_SILENT:
			   status = "SILENT";
		    // 무음 모드
			   break;
		}
		return status;
	}

	// 네트워크상태 (몇초후 실행,시스템종료 설정)
	public String Network_status(ConnectivityManager manager, WifiManager wifi) {
		String data = "off";
		int check;
		boolean isMobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
		boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();

		if (isWifi) {
			check = wifi.getWifiState();
			WifiInfo wInfo = wifi.getConnectionInfo();
			if (!wifi.isWifiEnabled()) {// 상태 확인(비활성)
				if (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
					wifi.setWifiEnabled(true); // 활성화 시키기(비활성일때)
				}
			}

			if (check == WifiManager.WIFI_STATE_ENABLED && wInfo.getSSID() != null) {
				data = "on";
			} else {
				if (isMobile) {
					data = "on";
				} else {
					data = "off";
				}
			}
		} else {
			if (isMobile) {
				data = "on";
			} else {
				data = "off";
			}
		}
		return data;
	}
	
	//WIFI 가능 여부
    public static boolean IsWifiAvailable(Context context)
    	{
    		ConnectivityManager m_NetConnectMgr= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    		boolean bConnect = false;
    		try
    		{
    			if( m_NetConnectMgr == null ) return false;

    			NetworkInfo info = m_NetConnectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    			bConnect = (info.isAvailable() && info.isConnected());

    		}
    		catch(Exception e)
    		{
    			return false;
    		}

    		return bConnect;
    	}

    //3G 가능 여부
    public static boolean Is3GAvailable(Context context)
    	{  
    		ConnectivityManager m_NetConnectMgr= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    		boolean bConnect = false;
    		try
    		{
    			if( m_NetConnectMgr == null ) return false;
    			NetworkInfo info = m_NetConnectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    			bConnect = (info.isAvailable() && info.isConnected());
    		}
    		catch(Exception e)
    		{
    			return false;
    		}

    		return bConnect;
    	}
    
	public static boolean unzip(File zipFile, File targetDir) {
        FileInputStream fis = null;
        ZipInputStream zis = null;
        ZipEntry zentry = null;


        try {
			try {


				fis = new FileInputStream(zipFile);
				zis = new ZipInputStream(fis);


				while ((zentry = zis.getNextEntry()) != null) {
					String fileNameToUnzip = zentry.getName(); 


					File targetFile = new File(targetDir, fileNameToUnzip);


					if (zentry.isDirectory()) {//if is Directory
						File path = new File(targetFile.getAbsolutePath());
						if (!path.isDirectory()) {
							path.mkdirs();
						}
					} else { //if is File 
						File path = new File(targetFile.getParent());
						if (!path.isDirectory()) {
							path.mkdirs();
						}
						unzipEntry(zis, targetFile);
					}
				}
			} finally {
				if (zis != null) {
					zis.close();
				}
				if (fis != null) {
					fis.close();
				}
			}
		} catch (Exception e) {
			return false;
		}
		return true;
    }
	
    private static File unzipEntry(ZipInputStream zis, File targetFile) throws Exception {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(targetFile);


            byte[] buffer = new byte[BUFFER_SIZE];
            int len = 0;
            while ((len = zis.read(buffer)) != -1) {
            	if(len == 0){
            		return null;
            	}
                fos.write(buffer, 0, len);
            }
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
        return targetFile;
    }
    
    // 폰해상도 가져오기
 	public int phone_display(Display display, String dispaly_w_h) {
 		int result_value = 0;
 		// Display display =
 		// ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
 		int displayWidth = display.getWidth();
 		int displayHeight = display.getHeight();
 		int displayOrientation = display.getOrientation();
 		int displayPixelFormat = display.getPixelFormat();

 		if (dispaly_w_h.equals("W")) {
 			result_value = displayWidth;
 		} else if (dispaly_w_h.equals("H")) {
 			result_value = displayHeight;
 		} else if (dispaly_w_h.equals("O")) {
 			result_value = displayOrientation;
 		} else if (dispaly_w_h.equals("P")) {
 			result_value = displayPixelFormat;
 		}
 		// float displayRefreshRate = display.getRefreshRate();
 		return result_value;
 	}

 	// 폰번호가져오기
 	public String phone_number(TelephonyManager systemService) {
 		String PhoneNumber = "";
 		PhoneNumber = systemService.getLine1Number();
 		return PhoneNumber;
 	}

 	// 폰모델가져오기
 	public String phone_model() {
 		String PhoneModel = "";
 		PhoneModel = (Build.MODEL).replace(" ", "-");
 		return PhoneModel;
 	}
}
